<?php defined('BASEPATH') or exit('No direct script access allowed');
/**
 *
 * @author  PyroCMS Dev Team
 * @package PyroCMS\Core\Modules\Blog\Controllers
 */
class Admin extends Admin_Controller
{

	public function __construct()
	{
		parent::__construct();
		$this->db->dbprefix="l_";
		$this->load->model('report_m');
		$this->load->model('bettype_m');
		$this->load->model('channel_m');
		$this->load->model('user_m');
		$this->load->model('statement_m');
		$this->load->model('transfer_m');
		$this->load->model('statistic_m');
	}

	public function initHtml(){
		$this->load->helper('url');
		$this->lang->load('report');
		$this->template
		->append_css('module::report.css')
		->append_css('libs/ui.jqgrid.css')
		->append_css('libs/960_24_col.css')
		->append_css('libs/daterangepicker.css')
		->append_js('libs/jquery.blockUI.js')
		->append_js('libs/grid.locale-en.js')
		->append_js('libs/jquery.jqGrid.js')
		->append_js('libs/date.js')
		->append_js('libs/daterangepicker.js')
		->append_js('module::report.js');
	}
	/**
	 * Show all created blog posts
	 */

	public function index()
	{
		$this->initHtml();

		$model = new Statistic_m();

		$this->template
		->set('userStatistic',$model->getUserStatistic())
		->set('runningBetStatistic',$model->getRunningBetStatistic())
		->set('todayWinLoseStatistic',$model->getTodayWinLoseStatistic())
		->set('currentMonthWinLoseStatistic',$model->getCurrentMonthWinLoseStatistic())
		->set('todayTransferStatistic',$model->getTodayTransferStatistic())
		->set('currentMonthTransferStatistic',$model->getCurrentMonthTransferStatistic())
		->build('admin/index');
	}

	public function totalBets()
	{
		$this->initHtml();

		$channelModel =new Channel_m();
		$date =new DateTime();
		$channelList = $channelModel->getAllByDate($date);
		$betTypeModel=new Bettype_m();
		$betTypeList=$betTypeModel->get_all();

		$this->template
		->append_js('module::betlist.js')
		->set('channelList',$channelList)
		->set('betTypeList',$betTypeList)
		->build('admin/totalbets');
	}


	public function betlist()
	{
		$this->initHtml();

		// Params
		$fromdate=isset($_REQUEST['fromdate'])?date_create_from_format('Y-m-d',$_REQUEST['fromdate']):(new DateTime());
		$todate=isset($_REQUEST['todate'])?date_create_from_format('Y-m-d',$_REQUEST['todate']):(new DateTime());
		$userId=isset($_REQUEST['user_id'])?(int)$_REQUEST['user_id']:0;
		$username=isset($_REQUEST['username'])?$_REQUEST['username']:"'";
		$metaModel=array(
			"fromdate"=>$fromdate,
			"todate"=>$todate,
			"userId"=>$userId,
			"username" =>$username
		);

		$this->template
		->append_js('module::betlist.js')
		->set("metaModel",$metaModel)
		->build('admin/betlist');
	}

	public function memberwl(){
		// Params
		$this->initHtml();
		$fromdate=isset($_REQUEST['fromdate'])?date_create_from_format('Y-m-d',$_REQUEST['fromdate']):(new DateTime());
		$todate=isset($_REQUEST['todate'])?date_create_from_format('Y-m-d',$_REQUEST['todate']):(new DateTime());
		$agentId=isset($_REQUEST['agent_id'])?$_REQUEST['agent_id']:0;
		$agentName=isset($_REQUEST['agent_name'])?$_REQUEST['agent_name']:"";
		$affiliateId =isset($_REQUEST['affiliate_id'])?$_REQUEST['affiliate_id']:0;
		$affiliateName=isset($_REQUEST['affiliate_name'])?$_REQUEST['affiliate_name']:"";
		$showFilter=isset($_REQUEST['show_filter'])?$_REQUEST['show_filter']=="true":true;
		$metaModel=array(
			"fromdate"=>$fromdate,
			"todate"=>$todate,
			"agentId"=>$agentId,
			"affiliateId"=>$affiliateId,
			"agentName"=>$agentName,
			"affiliateName"=>$affiliateName,
			"showFilter" =>$showFilter
		);

		$this->template
		->append_js('module::memberwl.js')
		->set('metaModel',$metaModel)
		->set('showFilter',$showFilter)
		->build('admin/memberwl');
	}

	public function agentwl(){
		// Params
		$this->initHtml();

		$metaModel=array(
			"fromdate"=>date('Y-m-d'),
			"todate"=>date('Y-m-d')
		);

		$this->template
		->append_js('module::agentwl.js')
		->set('metaModel',$metaModel)
		->build('admin/agentwl');
	}


	public function affiliatewl(){
		// Params
		$this->initHtml();

		$metaModel=array(
			"fromdate"=>date('Y-m-d'),
			"todate"=>date('Y-m-d')
		);

		$this->template
		->append_js('module::affiliatewl.js')
		->set('metaModel',$metaModel)
		->build('admin/affiliatewl');
	}

	public function balance()
	{
		// Params
		$this->initHtml();

		$this->template
		->append_js('module::balance.js')
		->build('admin/balance');
	}

	public function statement()
	{
		// Params
		$this->initHtml();
		$betTypeModel=new Bettype_m();
		$betTypeList=$betTypeModel->get_all();
		$this->template
		->append_js('module::betlist.js')
		->set('betTypeList',$betTypeList)
		->build('admin/statement');
	}

	// JSON
	public function totalBetsData()
	{
		// Params
		$fromdate =null;
		$todate =null;
		$completed=false;
		$userId=null;

		$transType=isset($_REQUEST['trans_type'])?$_REQUEST['trans_type']:0;
		$channelId =isset($_REQUEST['channel_id'])?$_REQUEST['channel_id']:0;

		$reportModel =new Report_m();
		$betList= $reportModel->getBetList($fromdate, $todate, $completed, $transType, $userId, $channelId);

		$stake =0;
		foreach($betList as $bet){
			$stake+=$bet->stake;
		}

		$result  = array(
			"Main"=>$betList,
			"Total"=>array("stake"=>$stake)
		);
		echo json_encode($result);
	}

	public function betlistData()
	{
		// Params
		$fromdate=isset($_REQUEST['fromdate'])?date_create_from_format('Y-m-d',$_REQUEST['fromdate']):(new DateTime());
		$todate=isset($_REQUEST['todate'])?date_create_from_format('Y-m-d',$_REQUEST['todate']):(new DateTime());

		$userId=isset($_REQUEST['user_id'])?(int)$_REQUEST['user_id']:0;
		$bettype=null;
		$channelId=null;
		$completed=true;
		$reportModel =new Report_m();
		$betList= $reportModel->getBetList($fromdate, $todate, $completed, $bettype, $userId, $channelId);

		$stake =0;
		$win_lose =0;
		$agent_win_lose=0;
		$affiliate_win_lose=0;
		$commission=0;
		$affiliate_commission=0;
		foreach($betList as $bet){
			$stake+=$bet->stake;
			$win_lose+=$bet->win_lose;
			$agent_win_lose+=$bet->agent_win_lose;
			$affiliate_win_lose+=$bet->affiliate_win_lose;
			$commission+=$bet->commission;
			$affiliate_commission+=$bet->affiliate_commission;
		}

		$result  = array(
			"Main"=>$betList,
			"Total"=>array("stake"=>$stake,
			"win_lose"=>$win_lose,
			"agent_win_lose"=>$agent_win_lose,
			"affiliate_win_lose"=>$affiliate_win_lose,
			"commission"=>$commission,
			"affiliate_commission"=>$affiliate_commission)
		);
		echo json_encode($result);

	}

	public function memberwlData(){
		// Params
		$fromdate=isset($_REQUEST['fromdate'])?date_create_from_format('Y-m-d',$_REQUEST['fromdate']):(new DateTime());
		$todate=isset($_REQUEST['todate'])?date_create_from_format('Y-m-d',$_REQUEST['todate']):(new DateTime());

		$agentId=isset($_REQUEST['agent_id'])?$_REQUEST['agent_id']:0;

		$affiliateId =isset($_REQUEST['affiliate_id'])?$_REQUEST['affiliate_id']:0;

		$reportModel =new Report_m();
		$memberWlData= $reportModel->getMemberWinLose($fromdate, $todate, (int)$agentId, (int)$affiliateId);
			
		$member_win_lose =0;
		$agent_win_lose=0;
		$affiliate_win_lose=0;
		$host_win_lose=0;
		foreach($memberWlData as $m){
			$member_win_lose +=$m->member_win_lose;
			$agent_win_lose +=$m->agent_win_lose;
			$affiliate_win_lose +=$m->affiliate_win_lose;
			$host_win_lose +=$m->host_win_lose;
		}

		$result  = array(
			"Main"=>$memberWlData,
			"Total"=>array(
				"member_win_lose"=>$member_win_lose,
				"agent_win_lose"=>$agent_win_lose,
				"affiliate_win_lose"=>$affiliate_win_lose,
				"host_win_lose"=>$host_win_lose
		)
		);
		echo json_encode($result);
	}

	public function agentwlData(){
		// Params

		$fromdate=isset($_REQUEST['fromdate'])?date_create_from_format('Y-m-d',$_REQUEST['fromdate']):(new DateTime());
		$todate=isset($_REQUEST['todate'])?date_create_from_format('Y-m-d',$_REQUEST['todate']):(new DateTime());
			
		$reportModel =new Report_m();
		$agentWlData= $reportModel->getAgentWinLose($fromdate, $todate);

		$agent_win_lose=0;
		$host_win_lose=0;
		foreach($agentWlData as $m){
			$agent_win_lose +=$m->agent_win_lose;
			$host_win_lose +=$m->host_win_lose;
		}

		$result  = array(
			"Main"=>$agentWlData,
			"Total"=>array(
				"agent_win_lose"=>$agent_win_lose,
				"host_win_lose"=>$host_win_lose
		)
		);
		echo json_encode($result);
	}

	public function affiliatewlData(){
		// Params

		$fromdate=isset($_REQUEST['fromdate'])?date_create_from_format('Y-m-d',$_REQUEST['fromdate']):(new DateTime());
		$todate=isset($_REQUEST['todate'])?date_create_from_format('Y-m-d',$_REQUEST['todate']):(new DateTime());

		$reportModel =new Report_m();
		$affiliateWlData= $reportModel->getAffiliateWinLose($fromdate, $todate);

		$affiliate_win_lose=0;
		$host_win_lose=0;
		foreach($affiliateWlData as $m){
			$affiliate_win_lose +=$m->affiliate_win_lose;
			$host_win_lose +=$m->host_win_lose;
		}

		$result  = array(
			"Main"=>$affiliateWlData,
			"Total"=>array(
				"affiliate_win_lose"=>$affiliate_win_lose,
				"host_win_lose"=>$host_win_lose
		)
		);
		echo json_encode($result);
	}

	public function balanceData(){
		// Params

		$userType=isset($_REQUEST['user_type'])?$_REQUEST['user_type']:'member';
		$agentId=isset($_REQUEST['agent_id'])?$_REQUEST['agent_id']:0;
		$affiliateId =isset($_REQUEST['affiliate_id'])?$_REQUEST['affiliate_id']:0;

		$reportModel =new Report_m();

		switch ($userType) {
			case 'agent':
				$balanceData=$reportModel->getAgentBalance();
				break;
			case 'affiliate':
				$balanceData=$reportModel->getAffiliateBalance();
				break;
			default:
				$balanceData=$reportModel->getMemberBalance($agentId, $affiliateId, null);
				break;
		}

		$result  = array(
			"Main"=>$balanceData,
			"Total"=>array()
		);

		echo json_encode($result);
	}

	public function statementData(){
		// Params

		$username=isset($_REQUEST['username'])?$_REQUEST['username']:'';
		$fromdate=isset($_REQUEST['fromdate'])?date_create_from_format('Y-m-d',$_REQUEST['fromdate']):(new DateTime());
		$todate=isset($_REQUEST['todate'])?date_create_from_format('Y-m-d',$_REQUEST['todate']):(new DateTime());
		$transtype=isset($_REQUEST['trans_type'])?$_REQUEST['trans_type']:'';

		$reportModel =new Report_m();
		$statmentModel =new Statement_m();
		$userModel=new User_m();

		$userInfo= $userModel->getUserInfo($username);

		if($userInfo&& $userInfo->user_id){
			$statementData =$statmentModel->getStatement($userInfo->user_id, $fromdate, $todate, $transtype);
			switch ($userInfo->user_type) {
				case 'AGENT':
					$balanceData=$reportModel->getAgentBalance($userInfo->user_id);
					break;
				case 'AFFILIATE':
					$balanceData=$reportModel->getAffiliateBalance($userInfo->user_id);
					break;
				default:
					$balanceData=$reportModel->getMemberBalance(null, null,$userInfo->user_id);
					break;
			}
			$balance = sizeof($balanceData)>0?$balanceData[0]->balance:0;
			$result  = array(
				"Main"=>$statementData,
				"Balance"=>$balance
			);
		}
		else {
			$result  = array(
			"Main"=>array(),
			"Balance"=>0
			);
		}
		echo json_encode($result);
	}

	// Transfer

	public function transfer(){
		$this->initHtml();

		$username=isset($_REQUEST['username'])?$_REQUEST['username']:'';
		$userList=array();
		if($username){
			$userModel=new User_m();
			$userList=$userModel->searchUser($username);
		}

		$this->template
		->append_js('module::transfer.js')
		->set('userList',$userList)
		->set('username',$username)
		->build('admin/transfer');
	}

	public function transferMoney(){
		$userId=isset($_REQUEST['userid'])?$_REQUEST['userid']:0;
		$amount =isset($_REQUEST['amount'])?$_REQUEST['amount']:0;

		$userModel=new User_m();
		$transferModel=new Transfer_m();
		$userInfo=$userModel->getUserInfoById($userId);
		if($amount<=0){
			$result=array("message"=>"Invalid amount");
		}
		elseif($userInfo&& $userInfo->user_id){
			$transferModel->transfer($userId, $amount, $userInfo->agent_id, $userInfo->affiliate_id);
			$result=array("message"=>"OK");
		}
		else{
			$result=array("message"=>"The user can not be found");
		}
			

		echo json_encode($result);
	}

	public function bonusMoney(){
		$userId=isset($_REQUEST['userid'])?$_REQUEST['userid']:0;
		$amount =isset($_REQUEST['amount'])?$_REQUEST['amount']:0;

		$userModel=new User_m();
		$transferModel=new Transfer_m();
		$userInfo=$userModel->getUserInfoById($userId);
		if($amount<=0){
			$result=array("message"=>"Invalid amount");
		}
		elseif($userInfo&& $userInfo->user_id){
			$transferModel->bonus($userId, $amount, $userInfo->agent_id, $userInfo->affiliate_id);
			$result=array("message"=>"OK");
		}
		else{
			$result=array("message"=>"The user can not be found");
		}
			

		echo json_encode($result);
	}

	public function transferreport(){
		// Params
		$this->initHtml();

		$metaModel=array(
			"fromdate"=>date('Y-m-d'),
			"todate"=>date('Y-m-d')
		);

		$this->template
		->append_js('module::transfer.js')
		->set('metaModel',$metaModel)
		->build('admin/transferreport');
	}

	public function transferreportData(){
		$fromdate=isset($_REQUEST['fromdate'])?date_create_from_format('Y-m-d',$_REQUEST['fromdate']):(new DateTime());
		$todate=isset($_REQUEST['todate'])?date_create_from_format('Y-m-d',$_REQUEST['todate']):(new DateTime());
		$transType=isset($_REQUEST['trans_type'])?$_REQUEST['trans_type']:'';

		$transferModel=new Transfer_m();
		$transferList = $transferModel->getTransferList($transType, $fromdate, $todate);


		$result  = array(
			"Main"=>$transferList,
			"Total"=>array()
		);

		echo json_encode($result);
	}
}
